//
//  ProgramCollectionViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/7.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ProgramCollectionViewCell.h"
#import "CFMacro.h"
#import "GXNetTool.h"

// model
#import "ProgramModel.h"
#import "Record_Plays.h"
#import "ProgramBanners.h"
#import "ProgramBannerTableViewCell.h"
#import "ProgramTableViewCell.h"
#import "Dj_Info.h"

#import "GXCycleImages.h"
#import "DJDetailBannerViewController.h"
#import "DJDetailViewController.h"

#define kProgramTableViewCell @"ProgramTableViewCell"
#define kProgramBannerTableViewCell @"ProgramBannerTableViewCell"

@interface ProgramCollectionViewCell () <UITableViewDelegate, UITableViewDataSource>
// tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIViewController *vc;

@end

@implementation ProgramCollectionViewCell

#pragma mark - 点击排序按钮
- (IBAction)upToDateButtonClicked:(id)sender {
    NSLog(@"最新排序");
}

#pragma mark - 点击分类按钮
- (IBAction)classifyButtonClicked:(id)sender {
    NSLog(@"广东分类");
}

#pragma mark - 点击地区按钮
- (IBAction)locationButtonClicked:(id)sender {
    NSLog(@"广东地区");
}

#pragma mark - init
- (void)awakeFromNib {
    [super awakeFromNib];
    
    // tableView
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // 注册正常cell
    kTalbleViewRegisterNib(@"ProgramTableViewCell", kProgramTableViewCell);
    // 注册轮播图cell
    kTalbleViewRegisterNib(@"ProgramBannerTableViewCell", kProgramBannerTableViewCell);
}

#pragma mark - model setter
- (void)setRoot:(ProgramModel *)root {
    _root = root;
    [self.tableView reloadData];
}

#pragma mark - tableView 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_root.record_plays) {
        return _root.record_plays.count;
    }
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.record_plays[indexPath.row].banners) {
        ProgramBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kProgramBannerTableViewCell];
        if (_root) {
            cell.banners = _root.record_plays[indexPath.row].banners;
            
#warning 这里有很多问题
            cell.cycleView.indexPath = ^(NSIndexPath *index) {
                switch (cell.banners[index.row - 1].banner_type) {
                    case 0: {
                        NSLog(@"type 0, 直接跳转播放页");
                    } break;
                    case 1: {
                        NSLog(@"type 1, 跳转节目详情页");
                        DJDetailBannerViewController *playerVC = [[DJDetailBannerViewController alloc] init];
                        playerVC.playKey = 119;
                        [self.vc.navigationController pushViewController:playerVC animated:YES];
                    } break;
                    case 2: NSLog(@"type 2"); break;
                    case 3: {
                        NSLog(@"type 3, 跳转djDetailVC");
                        DJDetailViewController *djDetailVC = [[DJDetailViewController alloc] init];
                        NSString *url = [NSString stringWithFormat:@"http://api.kufm.cn/v3/playback/get_dj_info?dj_id=287722&user_id=8839433563981"];
                        [GXNetTool GET:url andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
                            Dj_Info *DJ_Info = [Dj_Info modelWithDictionary:result];
                            DJ_Info.dj_id = 287722;
                            djDetailVC.dj_info = DJ_Info;
                        } andFailureBlock:^(NSError *error) {
                            NSLog(@"点击轮播图跳转请求数据失败");
                        }];
                        
                        
                        [self.vc.navigationController pushViewController:djDetailVC animated:YES];
                    } break;
                        
                    default:break;
                }
            };
        }
        return cell;
    } else {
        ProgramTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kProgramTableViewCell];
        if (_root) {
            cell.record_plays = _root.record_plays[indexPath.row];
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.record_plays[indexPath.row].banners) {
        return 120;
    } else if (indexPath.row == 0) {
        return 120;
    } else {
        return 80;
    }
}

- (UIViewController *)vc {
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    return (UIViewController *)responder;
}

@end
